<?php 
class htmlbox extends Module {
	function __construct(){
		$this->name = 'htmlbox';
		$this->tab = 'Other';
		$this->version = '1.3';
        $this->module_key = '';
        $this->dir = '/modules/htmlbox/';
		parent::__construct();
		$this->displayName = $this->l('HTMLbox');
		$this->description = $this->l('With this module you can put the HTML/JavaScript/CSS code anywhere you want');
        
	}
    
	function install(){
        if (parent::install() == false 
	    OR $this->registerHook('header') == false
	    OR $this->registerHook('top') == false
	    OR $this->registerHook('leftColumn') == false
	    OR $this->registerHook('rightColumn') == false
	    OR $this->registerHook('footer') == false
	    OR $this->registerHook('home') == false
        OR Configuration::updateValue('htmlbox_header', '0') == false
        OR Configuration::updateValue('htmlbox_top', '0') == false
        OR Configuration::updateValue('htmlbox_leftcol', '0') == false
        OR Configuration::updateValue('htmlbox_rightcol', '1') == false
        OR Configuration::updateValue('htmlbox_footercol', '0') == false
        OR Configuration::updateValue('htmlbox_homecol', '0') == false
        OR Configuration::updateValue('htmlbox_body', 'enter the code here') == false
        ){
            return false;
        }
        return true;
	}
    
	public function getContent(){
	   	$output="";
		if (Tools::isSubmit('submit_settings')){
			$body=str_replace(array("\rn", "\r", "\n"), array(' ',' ',' '),($_POST['htmlbox_body']));		
			Configuration::updateValue('htmlbox_body', $body, true);
            $output .= '<div class="conf confirm"><img src="../img/admin/ok.gif" alt="'.$this->l('Confirmation').'" />'.$this->l('Settings updated').'</div>';
 			
			$hooks=$this->getsettingsofhtmlbox();
			foreach ($hooks['hook'] AS $hook=>$value){
					if ($hook==$_POST['htmlbox_hook']){
						$hookval=1;
					} else {
						$hookval=0;
					}
					
					Configuration::updateValue($hook,$hookval);
			}                                    
        }	   
        $output.="";
        return $output.$this->displayForm();
	}

	public function displayForm(){
			global $cookie;
            $iso = Language::getIsoById((int)($cookie->id_lang));
            $isoTinyMCE = (file_exists(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en');
            $ad = dirname($_SERVER["PHP_SELF"]);

			$form='
			<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tiny_mce/tiny_mce.js"></script>
			<script type="text/javascript">
			var iso = \''.$isoTinyMCE.'\' ;
			var pathCSS = \''._THEME_CSS_DIR_.'\' ;
			var ad = \''.$ad.'\' ;
			
			$(document).ready(function(){
			tinySetup({
				editor_selector :"rte",
				theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull|cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,undo,redo",
				theme_advanced_buttons2 : "link,unlink,anchor,image,cleanup,code,|,forecolor,backcolor,|,hr,removeformat,visualaid,|,charmap,media,|,ltr,rtl,|,fullscreen",
				theme_advanced_buttons3 : "",
				theme_advanced_buttons4 : ""
				});
			});
			
			function addClass(){                    
				tinyMCE.execCommand(\'mceAddControl\', true, \'htmlbox_body\');
			}
			
			function removeClass(){
                  
				tinyMCE.execCommand(\'mceRemoveControl\', false, \'htmlbox_body\');
			}			
			
			</script>
			<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce.inc.js"></script>
			';
					
		$array=$this->getsettingsofhtmlbox();
		$radio="";   
		foreach ($array['hook'] as $key=>$value){
			if ($key=="htmlbox_leftcol"){$place=$this->l("left column");}
			if ($key=="htmlbox_rightcol"){$place=$this->l("right column");}
			if ($key=="htmlbox_top"){$place=$this->l("top");}
			if ($key=="htmlbox_header"){$place=$this->l("header");}
			if ($key=="htmlbox_footercol"){$place=$this->l("footer");}
			if ($key=="htmlbox_homecol"){$place=$this->l("home");}
			
			$selected=0;
			if ($value==1){
				$selected="checked";
			}
			$radio.="
				<tr>
					<td style=\"border-left:1px solid #c0c0c0; width:150px; padding:10px; background:#f2f2f2; border-bottom:1px solid #FFF; border-top:1px solid #c0c0c0; margin-right:10px;\">$place:</td>
					<td style=\"padding:10px; background:#fff; border-right:1px solid #c0c0c0;  border-bottom:1px solid #FFF; border-top:1px solid #c0c0c0; margin-right:10px;\"><input type=\"radio\" name=\"htmlbox_hook\" value=\"$key\" $selected style=\"cursor:pointer;\"/></td>
				</tr>
			";
		}
		return $form.'		
		<div style="diplay:block; clear:both; margin-bottom:20px;">
		<iframe src="http://mypresta.eu/content/uploads/2012/09/htmlbox_advertise.html" width="100%" height="130" border="0" style="border:none;"></iframe>
		</div>
		<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
            <div style="display:block; margin:auto; overflow:hidden; width:100%; vertical-align:top;">
                    <div style="clear:both; display:block; vertical-align:top;">
						<fieldset style="height:412px; display:inline-block; width:200px; margin-right:10px; vertical-align:top;">
							<legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('HTMLbox configuration').'</legend>   
          					<h3 style="margin-bottom:0px; padding-bottom:0px; margin-bottom:20px;">'.$this->l('Where to display HTMLbox ?').'</h3>
							<table style="border-bottom:1px solid #c0c0c0;  clear:both; vertical-align:top;" cellspacing="0" cellpadding="0">
							'.$radio.'
							</table>
						</fieldset>             
						
                        <fieldset style="display:inline-block; width:659px;  vertical-align:top;">
            				<legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('HTMLbox configuration').'</legend>
                            <h3 style="margin-bottom:0px; padding-bottom:0px;">'.$this->l('Enter the HTML/js/css code here').'</h3>
                            '.$this->l('editor').': 
                            <span onclick="$(\'#htmlbox_body\').addClass(\'rte\'); addClass();" style="cursor:pointer;">'.$this->l('On').'</span> |
                            <span onclick="$(\'#htmlbox_body\').removeClass(\'rte\'); removeClass();" style="cursor:pointer;">'.$this->l('Off').'</span>
                            <hr style="margin-top:5px;">
       						<textarea type="text" style="margin-bottom:10px; width:99%; height:300px;" id="htmlbox_body" name="htmlbox_body">'.$array['body'].'</textarea>                                                                                                                                         
                            <div align="center">
       				        <input type="submit" name="submit_settings" value="'.$this->l('Save Settings').'" class="button" />
                            </div>
                        </fieldset>                    
                    </div>
            </div>
		</form>
        ';
	}   
   
    public function getsettingsofhtmlbox(){
        $array['hook']['htmlbox_header']= Configuration::get('htmlbox_header');
		$array['hook']['htmlbox_top']= Configuration::get('htmlbox_top');
		$array['hook']['htmlbox_leftcol']= Configuration::get('htmlbox_leftcol');
		$array['hook']['htmlbox_rightcol']= Configuration::get('htmlbox_rightcol');
		$array['hook']['htmlbox_footercol']= Configuration::get('htmlbox_footercol');
		$array['hook']['htmlbox_homecol']= Configuration::get('htmlbox_homecol'); 
		$array['body'] = str_replace(array("\rn", "\r", "\n"), array(' ',' ',' '),Configuration::get('htmlbox_body'));
		return $array;
    }
   
	function hookrightColumn($params){
		$array=$this->getsettingsofhtmlbox();
		if ($array['hook']['htmlbox_rightcol']=="1"){
	        global $smarty;
	        $smarty->assign(array('htmlboxbody' => nl2br(stripslashes($array['body']))));
			return $this->display(__FILE__, 'html.tpl');	
		}
	}	 
    
	function hookleftColumn($params){
		$array=$this->getsettingsofhtmlbox();
		if ($array['hook']['htmlbox_leftcol']=="1"){
	        global $smarty;
	        $smarty->assign(array('htmlboxbody' => nl2br(stripslashes($array['body']))));
			return $this->display(__FILE__, 'html.tpl');
		}
	}
    
	function hookhome($params){
		$array=$this->getsettingsofhtmlbox();
		if ($array['hook']['htmlbox_homecol']=="1"){
	        global $smarty;
	        $smarty->assign(array('htmlboxbody' => nl2br(stripslashes($array['body']))));
			return $this->display(__FILE__, 'html.tpl');	
		}
	}
    
	function hookfooter($params){
		$array=$this->getsettingsofhtmlbox();
		if ($array['hook']['htmlbox_footercol']=="1"){
	        global $smarty;
	        $smarty->assign(array('htmlboxbody' => nl2br(stripslashes($array['body']))));
			return $this->display(__FILE__, 'html.tpl');
		}
	} 
	 
	function hookheader($params){
		$array=$this->getsettingsofhtmlbox();
		if ($array['hook']['htmlbox_header']=="1"){
	        global $smarty;
	        $smarty->assign(array('htmlboxbody' => nl2br(stripslashes($array['body']))));
			return $this->display(__FILE__, 'html.tpl');
		}
	}

	function hooktop($params){
		$array=$this->getsettingsofhtmlbox();
		if ($array['hook']['htmlbox_top']=="1"){
	        global $smarty;
	        $smarty->assign(array('htmlboxbody' => nl2br(stripslashes($array['body']))));
			return $this->display(__FILE__, 'html.tpl');
		}
	} 	      
    
}
?>